package com.futures.questionbank.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.futures.questionbank.R;
import com.futures.questionbank.adapter.BaseViewHolder;
import com.futures.questionbank.adapter.OptionsAdpter;
import com.futures.questionbank.base.BaseFragment;
import com.futures.questionbank.db.table.option;
import com.futures.questionbank.db.table.option_Table;
import com.futures.questionbank.db.table.problem;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.List;

public class Fragment_Control extends BaseFragment {
    private int position;
    List<problem> ivGoodsList;
    ViewPager viewPager;

    public Fragment_Control(int i, List<problem> problems, ViewPager viewPager) {
        super();
        this.position = i;
        this.ivGoodsList = problems;
        this.viewPager = viewPager;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout .activity_connect;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        TextView name = (TextView) rootView.findViewById(R.id.tv_1);
        TextView tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
        TextView tv_look = (TextView) rootView.findViewById(R.id.tv_look);
        TextView tv_2 = (TextView) rootView.findViewById(R.id.tv_2);
        TextView tv_num = (TextView) rootView.findViewById(R.id.tv_num);
        RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv);


        List<option> options = SQLite.select().
                from(option.class).where(option_Table.problem_id.eq(ivGoodsList.get(position).getId())).queryList();

        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        //设置Item增加、移除动画
        rv.setItemAnimator(new DefaultItemAnimator());
        name.setText(ivGoodsList.get(position).getType());
        tv_num.setText(position+1 + "/" + ivGoodsList.size());
        tvTitle.setText(ivGoodsList.get(position).getTitle());
        OptionsAdpter chapterAdpter = new OptionsAdpter(getActivity(), ivGoodsList.get(position).getMy_answer(),ivGoodsList.get(position).getType(),options, position, viewPager,new ProblemItemListener());
        rv.setAdapter(chapterAdpter);
        tv_look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_2.setVisibility(View.VISIBLE);
                tv_2.setText(ivGoodsList.get(position).getExplain());
            }
        });
    }


    class ProblemItemListener implements BaseViewHolder.onItemCommonClickListener {
        @Override
        public void onItemClickListener(int position) {
          //  chapterAdpter.setCheck(position);
          //  notifyDataSetChanged();
        }

        @Override
        public void onItemLongClickListener(int position) {

        }
    }
}
